package communication;

import model.Massage;
import util.ConversionUtil;
import util.DrawUtil;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;


@SuppressWarnings("all")
class ReadThread implements Runnable{

    private DataInputStream din;
    private String ip;
    private String name;

    public ReadThread(DataInputStream din, String ip, String name) {
        this.din = din;
        this.ip = ip;
        this.name = name;
    }

    @Override
    public void run() {
        ArrayList<Massage> list = new ArrayList<>();
        int num = 5000;
        byte[] bytes = new byte[16];
        int len = 0;
        while(true){
            try {
                //但返回值为-1的时候说明数据读取结束
                while ((len = din.read(bytes)) != -1){
                    double[] array = ConversionUtil.asDoubleArray(bytes);
                    Massage massage = new Massage();
                    massage.setX_car(array[0]);
                    massage.setTheta(array[1]);
                    //打印服务端发来的数据
                    //System.out.println("["+ip+"]"+name+"客户端"+":"+ Arrays.toString(array));
//                    list.add(massage);
//                    num -- ;
//                    if(num==0){
//                        DrawUtil.plot(list,0.002);
//                    }
                }
            } catch (IOException e) {
                break;
            }
        }
    }

}